using ApplicationBase.Core;
using ApplicationBase.Core.Web.Extensions;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using IdentityService.Application;
using IdentityService.Application.Behaviours;
using IdentityService.Domain.Aggregates.UserAccountAggregate;
using IdentityService.Domain.Common;
using IdentityService.Infrastructure;
using IdentityService.Infrastructure.Dtos;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Serilog;
using System.Globalization;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDefaultControllers();
builder.Services.AddDefaultSwagger();
builder.Services.AddDefaultJWT(builder.Configuration);

builder.Services.AddMemoryCache();

//AutoFac start
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder =>
{
    containerBuilder.RegisterModule<BaseModule>();
    containerBuilder.RegisterModule<AppModule>();
});
//AutoFac end

builder.Services.Configure<IdentityOptions>(options =>
{
    options.Password = new PasswordFormatOptions();
    options.Lockout = new AccountLockoutOptions();
});

//DbContext
builder.Services.AddDbContext<AppIdentityDbContext>(options =>
{
    //options.UseSqlServer("Server=.;Initial Catalog=IdentityService;Integrated Security=SSPI;Encrypt=True;TrustServerCertificate=True");
    options.UseSqlServer("Server=39.107.140.217,1433;Initial Catalog=IdentityService;User=sa;Password=Yez123!!;TrustServerCertificate=True;");
});

//MediatR start
builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssembly(typeof(TransactionBehaviour<,>).Assembly);
    cfg.AddOpenBehavior(typeof(PermitCheckBehaviour<,>));
    cfg.AddOpenBehavior(typeof(LoggerBehaviour<,>));
    cfg.AddOpenBehavior(typeof(TransactionBehaviour<,>));
});
//MediatR end

//Serilog start
Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .WriteTo.File("Log/log.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();
builder.Host.UseSerilog();
//Serilog end

//Localization start
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultureInfo = new List<CultureInfo>
    {
        new CultureInfo("zh-Hans"),
        new CultureInfo("en-US")
    };
    options.DefaultRequestCulture = new RequestCulture("zh-Hans");
    options.SupportedCultures = supportedCultureInfo;
});
builder.Services.AddLocalization();
//Localization end

//AutoMapper
builder.Services.AddAutoMapper(config =>
{
    config.CreateMap<UserAccount, UserAccountDto>();
});

//Cors
builder.Services.AddCors(options =>
{
    options.AddPolicy("cors", it => it.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());
});

//Register Middlewares
//builder.Services.AddSingleton<IAuthorizationMiddlewareResultHandler, AppAuthorizationMiddlewareResultHandler>();

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();

app.UseCors("cors");
app.UseHttpsRedirection();

var localizationOptions = app.Services.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(localizationOptions!.Value);

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();
